/* 
Set directory 
*/

    global root ENTER DIRECTORY

	global r4rawyc 		$root\Data\Young Lives\R4\UKDA-7931-stata11\stata11\india_r4\oc_ch_anon
	global compositer4 	$root\Data\Young Lives\R4\UKDA-7931-stata11\stata11\india_r4\oc_hh_anon
	global r3rawoc 		$root\Data\Young Lives\R3\survey_data_r3\india_r3\olderchild
	global compositer3 	$root\Data\Young Lives\R3\composite_scores_r3\india_r3
	global rawr2 		$root\Data\Young Lives\R2\survey_data_r2\india_r2\Older Child
	global compositer2 	$root\Data\Young Lives\R2\composite_scores_r2\india_r2
	global r1 			$root\Data\Young Lives\R1\survey_data_r1\india_r1
	global data 		$root\Dta\Young Lives\

*************************************************
*	MERGING\APPENDING RELEVANT VARIABLES
*************************************************
*Wave 4

use "$r4rawyc\in_r4_occh_olderchild.dta", clear
rename *, lower
gen tempin = "IN"
gen tempino = "IN0"
egen childid_1 = concat(tempin childcode)
egen childid_2 = concat(tempino childcode)
gen childid = childid_1
replace childid = childid_2 if childcode<99999
keep childid  zhfa zbfa fhfa fbfa bmi enrschr4 agemon sleepr4 crothr4 dmtskr4 tsfarmr4 actpayr4 atschr4 studygr4 lsurer4 tmcmwkr4 comschr4

gen round = 5

save "$data\in_r4_occh_olderchild.dta", replace	

use "$compositer4\in_r4_ochh_olderhousehold.dta", clear
rename *, lower
gen tempin = "IN"
gen tempino = "IN0"
egen childid_1 = concat(tempin childcode)
egen childid_2 = concat(tempino childcode)
gen childid = childid_1
replace childid = childid_2 if childcode<99999
keep childid hhsize hq cd elecq toiletq drwaterq cookingq sv wi

gen round = 5

save "$data\in_r4_ochh_olderhousehold.dta", replace

*************************************************
*Wave 3

use "$r3rawoc\in_oc_childlevel.dta", clear
rename *, lower

keep childid sleepr3 crothr3 dmtskr3 tsfarmr3 actpayr3 atschr3 studygr3 lsurer3 bmi chsex fbfa fhfa fwfa zbfa zhfa zwfa enrschr3

gen round = 3
save "$data\in_oc_childlevel.dta", replace	

use "$r3rawoc\in_oc_householdlevel.dta", clear
rename *, lower

keep childid clustid commid region typesite agechild foodexp nfoodexp totalexp  ///
foodexp_pc nfoodexp_pc totalexp_pc foodexp_r nfoodexp_r totalexp_r foodexp_rpc nfoodexp_rpc totalexp_rpc hhsize sex hq cd elecq drwaterq toiletq cookingq sv wi

gen round = 3

save "$data\in_oc_householdlevel.dta", replace
*************************************************
*Wave 2
use "$rawr2\inchildquest12yrold.dta", clear
rename *, lower

keep childid enrsch

gen round = 2

save "$data\inchildquest12yrold.dta", replace

use "$rawr2\inchildlevel12yrold.dta", clear
rename *, lower

keep childid sex hhsize commid typesite region mumed daded ///
agechild bmi zwfa zhfa zbfa fwfa fhfa fbfa foodexp nfoodexp totalexp foodexp_pc ///
nfoodexp_pc totalexp_pc foodexp_r nfoodexp_r totalexp_r foodexp_rpc nfoodexp_rpc totalexp_rpc hq cd elecq toiletq drwaterq cookingq sv wi 

save "$data\inchildlevel12yrold.dta", replace

use "$rawr2\insubhouseholdmember12.dta", clear
rename *, lower

keep if relate == 0

keep childid sleep chcare hhchore npaywork paywork school study play

gen round = 2
save "$data\insubhouseholdmember12.dta", replace

*************************************************
*Wave 1
use "$r1\inchildlevel8yrold.dta", clear
rename *, lower

keep childid sex clustid commid region typesite hhsize schnow ///
chldrel chldeth agechild ageorder zwfa zhfa zbfa wi bmi fwfa fhfa fbfa hq cd elecq toiletq drwaterq cookingq sv

gen round = 1
save "$data\inchildlevel8yrold.dta", replace

*****************************************************
*Merge Within Waves
*Wave 2
use "$data\inchildquest12yrold.dta", clear
merge 1:1 childid using "$data\inchildlevel12yrold.dta"
keep if _m == 3
drop _m
merge 1:1 childid using "$data\insubhouseholdmember12.dta"
keep if _m == 3
drop _m
save "$data\oth_ocwave2.dta", replace

*Wave 3
use "$data\in_oc_childlevel.dta", clear

merge m:1 childid using "$data\in_oc_householdlevel.dta"
keep if _m == 3
drop _m

save "$data\oth_ocwave3.dta", replace

*Wave 4
use "$data\in_r4_occh_olderchild.dta", clear
merge 1:1 childid using "$data\in_r4_ochh_olderhousehold.dta"
keep if _m == 3
drop _m
save "$data\oth_ocwave4.dta", replace

*Append Waves
use "$data\inchildlevel8yrold.dta", clear

append using "$data\oth_ocwave2.dta"
append using "$data\oth_ocwave3.dta"
append using "$data\oth_ocwave4.dta"

gen cohort = 1

save "$data\OC R1234.dta", replace



